package com.bringway.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.AppRootService;
import com.bringway.p2p.service.SecurityService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/wx")
public class WxController extends JsonController{
	
	Logger logger = Logger.getLogger(WxController.class);

	@RequestMapping("/test")
	public ModelAndView test(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		String signature = RequestUtils.getStringParameter(request, "signature");
		String timestamp = RequestUtils.getStringParameter(request, "signature");
		String nonce = RequestUtils.getStringParameter(request, "nonce");
		String echostr = RequestUtils.getStringParameter(request, "echostr");
		logger.info("signature : " + signature);
		logger.info("timestamp : " + timestamp);
		logger.info("nonce : " + nonce);
		logger.info("echostr : " + echostr);
		
		result.put("res", "1");
		serialize(request,response,result);
		return null;
	}
	

	
	/**
	 * 
	 * @param request
	 * @param response 查询数据
	 * @return
	 * @throws Exception
	 */
	@Autowired
	private AppRootService appRootService;
	@RequestMapping("/queryAppRoot")
	public ModelAndView queryAppRoot(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String yhm= RequestUtils.getStringParameter(request, "yhm");
		String xm= RequestUtils.getStringParameter(request, "xm");
		params.put("start", start);
		params.put("limit", limit);
		params.put("yhm", yhm);
		params.put("xm", xm);
		List<Map<String, Object>> list = appRootService.queryapproot(params);
		int count = appRootService.getAppRootCount(params);
		result.put("data", list);	
		result.put("total", count);
		serialize(request,response,result);
		return null;
	}
	
	
	/*public static boolean check_signature(String signature, String timestamp,String nonce) {
	    String[] arr = new String[] { timestamp, nonce, token };
	    Arrays.sort(arr);
	    String s = arr[0] + arr[1] + arr[2];
	    MessageDigest md = MessageDigest.getInstance("SHA-1");
	    byte[] digest = md.digest(s.getBytes("utf-8"));
	    return signature == bytes2HexString(digest);
	}*/
	
}
